/*
    This file is part of ciberRatoToolsSrc.

    Copyright (C) 2001-2006 Universidade de Aveiro

    ciberRatoToolsSrc is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    ciberRatoToolsSrc is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "crqrobotinfo.h"
#include <qlabel.h>
#include <qvbox.h>
#include <qlcdnumber.h>
#include <qpixmap.h>
#include <iostream>
#include <qpixmap.h>
#include <qwmatrix.h>

#define COLOR 200

using namespace std;

CRQRobotInfo::CRQRobotInfo( CRRobot *rob, QString skinName, 
  QWidget * parent = 0, const char *name = 0 )
  :QWidget( parent, name )

	{
	skin(skinName); /* Change skin */
	robot = rob;
	// ICON
	QLabel *icon = new QLabel( this );
	icon->setGeometry(0, 0, 50, 50 ); 
	icon->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
	
	switch ( rob->id() )
		{
		case 1:
			{
			QWMatrix mat;
			QPixmap tmpPix;
			QPixmap *pixx;
			pixx = new QPixmap(rob1File);
			mat.rotate(-90);
			tmpPix = pixx->xForm(mat);
			icon->setPixmap( tmpPix );
			break;
			}

		case 2:
			{
			QWMatrix mat2;
			QPixmap tmpPix2;
			QPixmap *pixx2;
			pixx2 = new QPixmap(rob2File);
			mat2.rotate(-90);
			tmpPix2 = pixx2->xForm(mat2);
			icon->setPixmap(tmpPix2);
			break;
			}

		case 3:
			{
			QWMatrix mat3;
			QPixmap tmpPix3;
			QPixmap *pixx3;
			pixx3 = new QPixmap(rob3File);
			mat3.rotate(-90);
			tmpPix3 = pixx3->xForm(mat3);
			icon->setPixmap(tmpPix3);
			break;
			}

		}

	icon->setBackgroundColor( QColor(COLOR, COLOR, COLOR) );
	//icon->setBackgroundOrigin( WindowOrigin );	
	//icon->setBackgroundPixmap( QPixmap(backFile) );
	// NOME
	QLabel *robname = new QLabel( this );
	robname->setText( robot->name() );
	robname->setAlignment( Qt::AlignLeft | Qt::AlignVCenter );
	robname->setFont( QFont( "Courier", 20, QFont::Bold ) );
	robname->setGeometry(60, 0, 250, 50 ); 
	QPalette pal;
	QColorGroup cg;
	cg.setColor( QColorGroup::Foreground, QColor( 0, 0, 0 ) );
	cg.setColor( QColorGroup::Background, QColor( COLOR, COLOR, COLOR ) );
	pal.setActive( cg );
	pal.setInactive( cg );
	robname->setPalette( pal );
	//robname->setBackgroundOrigin( WindowOrigin );	
	//robname->setBackgroundPixmap( QPixmap(backFile) );
	// RETURN TIME
	QLCDNumber *returnTime = new QLCDNumber( this, "Score2" );
	returnTime->setBackgroundColor( QColor(COLOR, COLOR, COLOR) );
	returnTime->setFrameStyle(QFrame::NoFrame);
	//returnTime->setBackgroundOrigin( WindowOrigin );	
	//returnTime->setBackgroundPixmap( QPixmap(backFile) );
	returnTime->setDecMode();
	returnTime->setGeometry( 325, 0, 75, 50 );
	returnTime->setNumDigits( 4 );
	returnTime->setSegmentStyle( QLCDNumber::Flat );
	returnTime->setPalette( pal );
	connect(this, SIGNAL(time(int)), returnTime, SLOT(display(int)));
	// SCORE
	QLCDNumber *score = new QLCDNumber( this, "Score" );
	//score->setBackgroundColor( white );
	score->setFrameStyle(QFrame::NoFrame);
	//score->setBackgroundOrigin( WindowOrigin );	
	//score->setBackgroundPixmap( QPixmap(backFile) );
	score->setDecMode();
	score->setGeometry( 610, 0, 75, 50 );
	score->setNumDigits( 4 );
	score->setSegmentStyle( QLCDNumber::Flat );
	score->setPalette( pal );
	connect( this, SIGNAL( score(int) ), score, SLOT( display(int) ) );
	// COLISOES
	QLCDNumber *collisions = new QLCDNumber( this, "Collision" );
	collisions->setBackgroundColor( QColor(COLOR, COLOR, COLOR) );
	collisions->setFrameStyle(QFrame::NoFrame);
	//collisions->setBackgroundOrigin( WindowOrigin );	
	//collisions->setBackgroundPixmap( QPixmap(backFile) );
	collisions->setDecMode();
	collisions->setGeometry( 445, 0, 60, 50 );
	collisions->setNumDigits( 3 );
	collisions->setSegmentStyle( QLCDNumber::Flat );
	QColorGroup cg1;
	cg1.setColor( QColorGroup::Foreground, QColor( 255, 186, 52 ) );
    cg1.setColor( QColorGroup::Background, QColor( 0, 0, 0 ) );
	pal.setActive( cg1 );
    pal.setInactive( cg1 );
	collisions->setPalette( pal );
	connect(this, SIGNAL(collisions(int)), collisions, SLOT(display(int)));
	// ESTADO
	previousState = CRRobot::STOPPED;
	QLabel *state = new QLabel( this );
	state->setGeometry(535, 0, 50, 50 ); 
	state->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
	state->setPixmap( QPixmap( stopFile ) );
	state->setBackgroundColor( QColor(COLOR, COLOR, COLOR) );
	//state->setBackgroundOrigin( WindowOrigin );	
	//state->setBackgroundPixmap( QPixmap(backFile) );
	connect( this, SIGNAL( state(const QPixmap &) ), state,
	  SLOT( setPixmap( const QPixmap &) ) );
	setFixedSize( 715, 50 );	
	//setBackgroundColor( white );
	//setBackgroundOrigin( ParentOrigin );	
	setBackgroundColor(QColor(COLOR, COLOR, COLOR));
	//setBackgroundPixmap( QPixmap(backFile) );
}

//-----------------------------------------------------------------------------

CRQRobotInfo::~CRQRobotInfo()

	{

	}

//-----------------------------------------------------------------------------

void CRQRobotInfo::skin(QString skinName)

	{
	strcpy(backFile, "skins/");
	strcat(backFile, skinName);
	strcat(backFile, "/robbg.png");

	strcpy(rob1File, "skins/");
	strcat(rob1File, skinName);
	strcat(rob1File, "/rob1/rob.png");

	strcpy(rob2File, "skins/");
	strcat(rob2File, skinName);
	strcat(rob2File, "/rob2/rob.png");

	strcpy(rob3File, "skins/");
	strcat(rob3File, skinName);
	strcat(rob3File, "/rob3/rob.png");

	strcpy(runFile, "skins/");
	strcat(runFile, skinName);
	strcat(runFile, "/states/running.png");

	strcpy(stopFile, "skins/");
	strcat(stopFile, skinName);
	strcat(stopFile, "/states/stopped.png");

	strcpy(returnFile, "skins/");
	strcat(returnFile, skinName);
	strcat(returnFile, "/states/returning.png");

	strcpy(removeFile, "skins/");
	strcat(removeFile, skinName);
	strcat(removeFile, "/states/removed.png");

	strcpy(finishFile, "skins/");
	strcat(finishFile, skinName);
	strcat(finishFile, "/states/finished.png");
	}

//-----------------------------------------------------------------------------

void CRQRobotInfo::update( CRRobot * rob)

	{
	robot = rob; 
	emit score( (int) robot->score() );
	emit collisions( (int) robot->collisions() );
	emit returnTime( (int) robot->returnTime() );
//2005	if(robot->state() == CRRobot::RUNNING)
//2005		emit time( (int) robot->currentTime() );
	emit time( (int) robot->arrivalTime() );
		
	if( previousState != robot->state() )
		{
		previousState = robot->state();
		switch ( robot->state() )
			{
			case CRRobot::STOPPED:
				{
				emit state( QPixmap( stopFile ) );
				break;
				}
			
			case CRRobot::RUNNING:
				{
				emit state( QPixmap( runFile ) );
				break;
				}

			case CRRobot::REMOVED:
				{
				emit state( QPixmap( removeFile ) );
				break;
				}

			case CRRobot::FINISHED:
				{
				emit state( QPixmap( finishFile ) );
				break;
				}

			case CRRobot::RETURNING:
				{
				emit state( QPixmap( returnFile ) );
				break;
				}

			}

		}

	}
